var timeOut = 2000;

/**
 * 弹出窗口
 * 
 * @param url 窗口访问地址
 */
function openModal(url) {
	var random = Math.floor(Math.random()*100);
	var popId = "viewWindw_" + random;
	var templet = '<div id="' + popId +'" class="modal fade" aria-hidden="true" aria-labelledby="' + popId + 'Label' +'">'
	+ '<div class="modal-dialog">'
	+ '<div class="modal-content">'
	+ '<div class="modal-body"></div>'
	+ '</div></div></div>';
	$(document.body).append(templet);
	
	var w = "#" + popId;
	
	$(w).modal({
		remote : url
	});

	$(w).on("hidden.bs.modal", function() {
		$(this).removeData("bs.modal");
		$(w).find("div.modal-content").html("");
		$(w).remove();
	});
	
	return w;
}

/**
 * 文件上传完成处理函数
 * @param uploadJsonStrEl	上传字符串选择器名称
 * @param data				服务器端返回的数据
 */
function fileuploadedHandle(uploadJsonStrEl,data){
	var $uploadJsonStr = $(uploadJsonStrEl);
	// 截取
	var uploadFileStr = JSON.stringify(data.jqXHR.responseJSON.data);
	uploadFileStr = uploadFileStr.substring(1,uploadFileStr.length - 1);
	
	var oldUploadFileStr = $uploadJsonStr.val();
	if(oldUploadFileStr && $.trim(oldUploadFileStr).length > 0){
		$uploadJsonStr.val(oldUploadFileStr + "," + uploadFileStr);
	}else{
		$uploadJsonStr.val(uploadFileStr);
	}
}

/**
 * 文件删除完成处理函数
 * @param uploadJsonStrSelector	上传字符串选择器名称
 * @param removeKeyStrSelector	已删除的文件KEY选择器名称
 */
function filedeletedHandle(uploadJsonStrSelector,key,removeKeyStrSelector){
	var $uploadJsonStr = $(uploadJsonStrSelector);
	// 获取旧的上传JSON字符串
	var oldUploadFileStr = $uploadJsonStr.val();
	// 转换成JSON数组
	var uploadFileStrArr = eval("["+oldUploadFileStr+"]");
	// 过滤已删除的
	var newUploadFileArr = $.grep(uploadFileStrArr,function(json,i){
		return json["key"] != key;
	});
	// 转换为上传JSON字符串
	var newUploadFileStr = JSON.stringify(newUploadFileArr).replace("[","").replace("]","");
	$uploadJsonStr.val(newUploadFileStr);
	
	// 保存已删除的文件KEY
	var $removeKeyStr = $(removeKeyStrSelector);
	if(!$removeKeyStr){
		return;
	}
	
	var removeKeyVal = $removeKeyStr.val();
	if(removeKeyVal && $.trim(removeKeyVal).length > 0){
		removeKeyVal+=","+key;
	}else{
		removeKeyVal = key;
	}
	$removeKeyStr.val(removeKeyVal);
}

// 全局ajax完成事件
$(document).ajaxComplete(function(event, xhr, settings){
	if(typeof(xhr.responseText) != "undefined" && xhr.responseText.indexOf("login-logo") > 1){
		window.location.href = basePath+"login.htm";
	}
});
// 解决bootstrap模态框和select2无法获取焦点问题
$.fn.modal.Constructor.prototype.enforceFocus = function() {};